 tab_a_selector = 'li > a';
//$(function(){
//	init_tabs();	
//	
//	var tabs = $('.tabs');
//		$(window).bind( 'hashchange', function(e) {
//			 
//			var hashobject = $.bbq.getState();
//			
//			for (w in hashobject )
//			{
//				var idx = $.bbq.getState( w, true ) || 0;
//				var widget = $("ul.tabs#"+w+" > li > a" );
//				if(widget.size() != 0){
//					widget.eq( idx ).triggerHandler( 'change' );
//				}
//			}
//			
//			for (w in hashobject ){
//				var widget = $("#"+w);
//				if(widget.size() == 0){
//					 $.bbq.removeState(w);
//				}
//			}
//			
//			
//		});
//		  
//		 $(window).trigger( 'hashchange' );
//});

function init_tabs(sector){
	sector = sector == undefined? '' : sector
	$("ul.tabs"+sector+" > li > a").bind("change",function(){
		$(this).closest( 'ul.tabs').find('li').removeClass("active");
		$(this).parent().addClass("active");
		var activeTab = $(this).attr("href");
		var target_selector = "#"+$(this).closest( 'ul.tabs').attr('target');
		loadingTarget = target_selector;
		$(target_selector).load(activeTab);
		$(target_selector).fadeIn();
		return false;
	});  

	// The "tab widgets" to handle.
	var tabs = $('.tabs'+sector);
	
	// This selector will be reused when selecting actual tab widget A elements.
	
	
	// Enable tabs on all tab widgets. The `event` property must be overridden so
	// that the tabs aren't changed on click, and any custom event name can be
	// specified. Note that if you define a callback for the 'select' event, it
	// will be executed for the selected tab whenever the hash changes.
	tabs.tabs({ event: 'change' });
	
	// Define our own click handler for the tabs, overriding the default.
	tabs.find( tab_a_selector ).bind('click',function(){
	var state = {},
	  
	  // Get the id of this tab widget.
	  id = $(this).closest( '.tabs' ).attr( 'id' ),
	  
	  // Get the index of this tab.
	  idx = $(this).parent().prevAll().length;
	
	// Set the state!
	state[ id ] = idx;
	$.bbq.pushState( state );
	return false;
	});
}

function load_widget(keystate){
	var idx = $.bbq.getState( keystate, true ) || 0;
	init_tabs('#'+keystate);
	$( 'ul.tabs#'+keystate+' '+tab_a_selector ).eq( idx ).triggerHandler( 'change' );
}



